const state = {
    // 音乐播放的状态
    isPlaying:false,
    // 当前歌曲url
    songUrl:"",
    // 当前歌曲的详情
    nowSongDetail:localStorage.getItem("nowSongDetail") ? JSON.parse(localStorage.getItem("nowSongDetail")) : {},
    // 播放历史列表
    historyPlay:localStorage.getItem('historyPlay')? JSON.parse(localStorage.getItem('historyPlay')) : [],
    // 正在播放的列表
    playingList:localStorage.getItem("playingList") ? JSON.parse(localStorage.getItem("playingList")) : [],
    // 当前歌曲播放的实时秒数
    currentSecond:localStorage.getItem("currentSecond") ? JSON.parse(localStorage.getItem("currentSecond")) : 0,
    // // 存储当前观看的歌单的信息
    // songs:localStorage.getItem("songs") ? JSON.parse(localStorage.getItem("songs")) : [],
    // 是否显示当前歌曲详情页
	isShowSongDetail: false,
    // 当前播放歌曲的歌词
    currentLyric:[],
    // 歌词当前的行数
    currentRow:localStorage.getItem('currentRow')?JSON.parse(localStorage.getItem('currentRow')):0,
    // 是否展示浮动歌词
    // localStorage.getItem('isShowFloatLyric')?JSON.parse(localStorage.getItem('isShowFloatLyric')):
    isShowFloatLyric:false,

    
};

const mutations = {
    SAVESONGURL(state,songUrl){
        state.songUrl = songUrl;
    },
    CHANGEPLAYSTATE(state,isPlaying){
        state.isPlaying = isPlaying
    },
    SAVESONGDETAIL(state,nowSongDetail){
        state.nowSongDetail = nowSongDetail
        // 将当前播放的歌曲详情存储在本地
        localStorage.setItem("nowSongDetail", JSON.stringify(state.nowSongDetail));
        // 存储在播放历史中
        if(JSON.stringify(nowSongDetail) !== '{}'){
            let index = state.historyPlay.findIndex(item =>{
                return item.id == nowSongDetail.id;
            })
            if(index == -1){
                state.historyPlay.unshift(nowSongDetail);
                // 将播放历史存储在本地
                localStorage.setItem("historyPlay", JSON.stringify(state.historyPlay));
            }
        }
    },
    ADDPLAYINGLIST(state,song){
        // 检查歌曲是否已经存在在正在播放的列表中
        let index = state.playingList.findIndex(item =>{
            return item.id == song.id
        })
        if(index == -1){
            // 添加到列表中
            state.playingList.unshift(song);
            // 保存在本地
            localStorage.setItem('playingList',JSON.stringify(state.playingList))
        }
    },
    SAVECURRENTSECONDS(state,currentSecond){
        state.currentSecond = currentSecond;
        localStorage.setItem("currentSecond", JSON.stringify(state.currentSecond));
    },
    DELETESONG(state,index){
        // 删除仓库中的播放列表
        state.playingList.splice(index,1)
        // 更新 本地存储
        localStorage.setItem('playingList',JSON.stringify(state.playingList))
    },
    DELETEHISTORYSONG(state,index){
        state.historyPlay.splice(index,1);
        localStorage.setItem('historyPlay',JSON.stringify(state.historyPlay))
    },
    CLEARPLAYINGLIST(state){
        // 清空列表
        state.playingList = [];
        localStorage.removeItem('playingList')
        // 当前的播放歌曲的url
        state.songUrl = "";
        // 当前播放歌曲的详情
        state.nowSongDetail = {};
        localStorage.removeItem('nowSongDetail')
        // 当前播放的状态
        state.isPlaying = false;
        // 当前播放的秒数
        state.currentSecond = 0;   
        localStorage.removeItem('currentSecond')
        // 清空当前播放的时长 (分钟) 
        localStorage.removeItem('currentTime');
        // 清除当前播放音乐的总时长(秒)
        localStorage.removeItem('totalSecond')
    },
    CLEARHISTORYPLAY(state){
        state.historyPlay = [];
        localStorage.setItem('historyPlay',JSON.stringify(state.historyPlay))
    },
    SHOWSONGDETAIL(state){
        state.isShowSongDetail = !state.isShowSongDetail
    },
    SAVECURRENTLYRIC(state,lyric){
        state.currentLyric = lyric;
    },
    SAVECURRENTROW(state,currentRow){
        state.currentRow = currentRow;
        localStorage.setItem('currentRow',JSON.stringify(state.currentRow))
    },
    ISSHOWFLOATLYRIC(state,showLyric){
        state.isShowFloatLyric = showLyric;
        localStorage.setItem('isShowFloatLyric',JSON.stringify(state.isShowFloatLyric))
    },
    ADDALLSONG(state,songs){
        state.playingList = songs;
        localStorage.setItem('playingList',JSON.stringify(state.playingList))
    }
    
};

const actions = {
    // 保存歌曲的url
    saveSongUrl({commit},songUrl){
        commit('SAVESONGURL',songUrl);
    },
    // 改变歌曲的播放状态
    changePlayState({commit},isPlaying){
        commit('CHANGEPLAYSTATE',isPlaying)
    },
    // 保存当前播放歌曲的详情
    saveSongDetail({commit},songDetail){
        commit('SAVESONGDETAIL',songDetail)
    },
    // 添加歌曲进入播放列表
    addPlayinglist({commit},songDetail){
        commit('ADDPLAYINGLIST',songDetail)
    },
    // 保存当前歌曲播放的秒数
    saveCurrentSeconds({commit},currentSecond){
        commit('SAVECURRENTSECONDS',currentSecond)
    },
    // 删除播放列表单曲
    deleteSong({commit},index){
        commit('DELETESONG',index)
    },
    // 删除播放历史列表单曲信息
    deletehistorySong({commit},index){
        commit('DELETEHISTORYSONG',index)
    },
    // 清空播放列表
    clearPlayingList({commit}){
        commit('CLEARPLAYINGLIST')
    },
    // 清空历史记录
    clearHistoryPlay({commit}){
        commit('CLEARHISTORYPLAY')
    },
    // 更新是否展示当前播放歌曲的详情页
    showSongDetail({commit}){
        commit('SHOWSONGDETAIL')
    },
    // 保存整理好的歌词
    saveCurrentLyric({commit},lyric){
        commit('SAVECURRENTLYRIC',lyric)
    },
    // 保存当前播放的行数
    saveCurrentRow({commit},currentRow){
        commit('SAVECURRENTROW',currentRow)
    },
    // 保存是否展示悬浮歌词
    isShowLyric({commit},showLyric){
        commit('ISSHOWFLOATLYRIC',showLyric)
    },
    // 播放全部歌曲
    addAllSong({commit},songs){
        commit('ADDALLSONG',songs)
    }
};

const getters = {};

export default{
    state,
    mutations,
    actions,
    getters
}
